﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

namespace NJL.Web.Shop
{
    public partial class ShopClaim : BasePage
    {
        NJL.BLL.NJL_REG_COMPANY_APPLY bll = new BLL.NJL_REG_COMPANY_APPLY();
        NJL.Model.NJL_REG_COMPANY_APPLY model = new Model.NJL_REG_COMPANY_APPLY();
        NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        NJL.Model.NJL_COMPANY modelCompany;
        NJL.BLL.NJL_USER bllUser = new BLL.NJL_USER();
        NJL.Model.NJL_USER modelUser;
        NJL.BLL.NJL_PERSON bllPerson = new BLL.NJL_PERSON();
        NJL.Model.NJL_PERSON modelPserson;
        protected void Page_Load(object sender, EventArgs e)
        {
            modelCompany = bllCompany.GetModel(Request.QueryString["id"]);
        }
        protected void btnSubmint_Click(object sender, EventArgs e)
        {
            Boolean fileOK = false;
            modelUser = bllUser.GetModel(UserId);
            modelPserson = bllPerson.GetModelByUserId(UserId);
            model.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
            model.APPLY_TYPE = 1; //资料申领
            model.APPLY_COMPANY_ID = Request.QueryString["id"];
            model.APPLY_COMPANY_NAME = modelCompany.COMPANY_NAME;
            model.APPLY_CONTACT_NAME = modelPserson.REAL_NAME;
            model.MOBILE = modelUser.MOBILE;
            model.TEL = modelUser.TEL;
            model.EMAIL = modelUser.EMAIL;
            model.QQ = modelUser.QQ;
            model.MSN = modelUser.MSN;
            if (FileUpload1.HasFile)
            {
                String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                String[] allowedExtensions = { ".jpg", ".jpeg", ".gif" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i] && FileUpload1.PostedFile.ContentType.Contains("image"))
                    {
                        fileOK = true;
                        break;
                    }
                }
                if (fileOK)
                {
                    if (FileUpload1.PostedFile.ContentLength < 5242880)
                    {
                        try
                        {
                            model.APPLY_FILE_CODE_01 = base.SaveUploadFile(Request.Files, 0);
                            Thread.Sleep(1);
                        }
                        catch (Exception ex)
                        {
                            Royalsoft.Common.MessageBox.Show(this, "文件上传失败原因:" + ex.Message);
                        }
                    }
                    else
                    {
                        Royalsoft.Common.MessageBox.Show(this, "上传文件大小不能超过5M哦");
                    }
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "只能上传.jpg、.gif格式的图片文件哦");
                    return;
                }
            }
            else
            {
                Royalsoft.Common.MessageBox.Show(this, "请上传营业执照副本扫描件");
                return;
            }
            if (FileUpload2.HasFile)
            {
                String fileExtension = System.IO.Path.GetExtension(FileUpload2.FileName).ToLower();
                String[] allowedExtensions = { ".jpg", ".jpeg", ".gif" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i] && FileUpload2.PostedFile.ContentType.Contains("image"))
                    {
                        fileOK = true;
                        break;
                    }
                    else
                    {
                        fileOK = false;
                    }
                }
                if (fileOK)
                {
                    if (FileUpload1.PostedFile.ContentLength < 5242880)
                    {
                        try
                        {
                            model.APPLY_FILE_CODE_02 = base.SaveUploadFile(Request.Files, 1);
                            Thread.Sleep(1);
                        }
                        catch (Exception ex)
                        {
                            Royalsoft.Common.MessageBox.Show(this, "文件上传失败原因:" + ex.Message);
                        }
                    }
                    else
                    {
                        Royalsoft.Common.MessageBox.Show(this, "上传文件大小不能超过5M哦");
                    }
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "只能上传.jpg、.gif格式的图片文件哦");
                    return;
                }
            }
            else
            {
                Royalsoft.Common.MessageBox.Show(this, "请上传营业执照副本扫描件");
                return;
            }
            model.CREATE_USER_ID = UserId;
            model.CREATE_USER_NAME = UserName;
            model.CREATE_DATE_TIME = DateTime.Now;
            if (bll.Add(model))
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>window.parent.shopClaimsuccess()</script>");
            }
        }
    }
}